package com.woniu113.junk.site.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.scenario.effect.impl.prism.PrRenderInfo;
import com.woniu113.junk.commons.model.Result;
import com.woniu113.junk.site.client.UserClient;
import com.woniu113.junk.site.client.dto.AdminInfo;
import com.woniu113.junk.site.dto.AllFindAllWastePriceDTO;
import com.woniu113.junk.site.dto.FindAllWastePriceDTO;
import com.woniu113.junk.site.dto.WastePriceDTO;
import com.woniu113.junk.site.exception.WastePriceException;
import com.woniu113.junk.site.mapper.StationInfoMapper;
import com.woniu113.junk.site.mapper.WasteTypeMapper;
import com.woniu113.junk.site.model.StationInfo;
import com.woniu113.junk.site.model.WastePrice;
import com.woniu113.junk.site.mapper.WastePriceMapper;
import com.woniu113.junk.site.model.WasteType;
import com.woniu113.junk.site.param.wasteprice.AddWastePriceParam;
import com.woniu113.junk.site.param.wasteprice.FindAllWastePriceParam;
import com.woniu113.junk.site.param.wasteprice.FindWastePriceByTypeAndCityParam;
import com.woniu113.junk.site.service.StationInfoService;
import com.woniu113.junk.site.service.WastePriceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu113.junk.site.util.Data;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 蜡笔没了小新
 * @since 2024年10月16日
 */
@Service
public class WastePriceServiceImpl extends ServiceImpl<WastePriceMapper, WastePrice> implements WastePriceService {

    @Resource
    private WasteTypeMapper wasteTypeMapper;
    @Resource
    private WastePriceMapper wastePriceMapper;
    @Resource
    private StationInfoMapper stationInfoMapper;
    @Resource
    private StationInfoService stationInfoService;
    @Resource
    private UserClient userClient;

    @Override
    public WastePrice addWastePrice(AddWastePriceParam param, Integer adminId) {
        //1.判断传入数据是否完整
        if (param.getPrice()==null || param.getStationId() ==null || param.getTypeId()==null){
            throw new WastePriceException("请将数据填写完整",4001);
        }
        //2.判断废品价格是否合理
        if (param.getPrice().compareTo(BigDecimal.ZERO)<=0){
            throw new WastePriceException("废品价格不合理",4002);
        }
        //3.判断废品类别是否存在
        QueryWrapper<WasteType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_id",param.getTypeId()).ne("type_state", Data.WASTE_TYPE_STATE_DELETED);
        WasteType wasteType = wasteTypeMapper.selectOne(queryWrapper);
        if (wasteType==null){//为空表示没有找到符合条件的废品类型
            throw new WastePriceException("该废品类型不存在",4003);
        }
        //4.判断站点是否存在
        QueryWrapper<StationInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("station_id",param.getStationId()).ne("station_state",Data.STATION_STATE_CLOSE);
        StationInfo stationInfo = stationInfoMapper.selectOne(queryWrapper1);
        if (stationInfo==null){
            throw new WastePriceException("该站点不存在",4004);
        }
        //5.新增废品价格
        WastePrice wastePrice = new WastePrice();
        wastePrice.setPrice(param.getPrice());
        wastePrice.setAdminId(adminId);
        wastePrice.setStationId(param.getStationId());
        wastePrice.setTypeId(param.getTypeId());
        wastePrice.setLastUpdateTime(LocalDateTime.now());
        wastePriceMapper.insert(wastePrice);
        return wastePrice;
    }

    @Override
    public void updateWastePrice(BigDecimal price, Integer priceId, Integer adminId) {
        //1.判断数据完整性
        if (price==null || priceId==null){
            throw new WastePriceException("请将数据填写完整",4001);
        }
        //2.判断价格是否合理
        if (price.compareTo(BigDecimal.ZERO)<=0){
            throw new WastePriceException("废品价格不合理",4002);
        }
        //3.判断priceId对应的废品价格信息是否存在
        WastePrice wastePrice = wastePriceMapper.selectById(priceId);
        if (wastePrice==null){
            throw new WastePriceException("该废品价格数据不存在",4005);
        }
        //4.判断当前登录adminId是否有所属站点
        Result<AdminInfo> adminInfo = userClient.selectAdminById(adminId);
        if (adminInfo==null){
            throw new WastePriceException("当前登录用户没有所属站点",4006);
        }
        //5.判断当前登录adminId所属站点是否和原本的priceId对应的废品价格数据中的站点相同
        if (wastePrice.getStationId()!=adminInfo.getData().getStationId()){
            throw new WastePriceException("当前登录用户不能修改废品价格信息",4007);
        }
        //6.执行修改操作
        wastePrice.setPrice(price);
        wastePrice.setAdminId(adminId);
        wastePrice.setLastUpdateTime(LocalDateTime.now());
        wastePriceMapper.updateById(wastePrice);
    }

    @Override
    public void deleteWastePrice(Integer priceId) {
        //1.判断数据完整性
        if (priceId==null){
            throw new WastePriceException("请将数据填写完整",4001);
        }
        //2.执行删除
        int i = wastePriceMapper.deleteById(priceId);
        if (i!=1){//说明没能删除
            throw new WastePriceException("要删除的废品价格数据不存在",4008);
        }
        //否则则删除成功
    }

    @Override
    public WastePriceDTO findWastePriceByTypeAndCity(FindWastePriceByTypeAndCityParam param) {
        //1.判断数据完整性
        if (param.getCity()==null||param.getCounty()==null||param.getTypeId()==null){
            throw new WastePriceException("请将数据填写完整",4001);
        }
        //2.根据城市和区县查询区站点id
        StationInfo station = stationInfoService.findFatherStationByCityAndCounty(param.getCity(), param.getCounty());
        if (station==null){
            throw new WastePriceException("该站点不存在",4004);
        }
        //3.根据区站点id查询出对应废品类型的价格信息
        QueryWrapper<WastePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",station.getStationId()).eq("type_id",param.getTypeId());
        WastePrice wastePrice = wastePriceMapper.selectOne(queryWrapper);
        if (wastePrice==null){
            throw new WastePriceException("该废品价格数据不存在",4005);
        }
        //4.查询废品名
        WasteType wasteType = wasteTypeMapper.selectById(wastePrice.getTypeId());
        String typeName = wasteType.getTypeName();
        //5.返回dto
        WastePriceDTO wastePriceDTO = new WastePriceDTO();
        wastePriceDTO.setTypeId(wastePrice.getTypeId());
        wastePriceDTO.setStationId(wastePrice.getStationId());
        wastePriceDTO.setPrice(wastePrice.getPrice());
        wastePriceDTO.setTypeName(typeName);
        return wastePriceDTO;
    }

    @Override
    public List<WastePrice> findByCStationId(Integer infoStationId) {
        QueryWrapper<WastePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("station_id",infoStationId);
        List<WastePrice> list = wastePriceMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public AllFindAllWastePriceDTO findAll(FindAllWastePriceParam param, Integer offset) {
        QueryWrapper<WastePrice> queryWrapper = new QueryWrapper<>();
        if (param.getStationId()!=null){
            queryWrapper.eq("station_id",param.getStationId());
        }
        if (param.getTypeId()!=null){
            queryWrapper.eq("type_id",param.getTypeId());
        }
        if (param.getMinPrice()!=null){
            queryWrapper.gt("price",param.getMinPrice());
        }
        if (param.getBigPrice()!=null){
            queryWrapper.lt("price",param.getBigPrice());
        }
        int size =Math.toIntExact(wastePriceMapper.selectCount(queryWrapper));
        queryWrapper.last("LIMIT " + offset + ", " + param.getPageSize());
        List<WastePrice> infos = wastePriceMapper.selectList(queryWrapper);
        ArrayList<FindAllWastePriceDTO> wastePriceDTOS = new ArrayList<>();
        for (WastePrice info : infos) {
            FindAllWastePriceDTO findAllWastePriceDTO = BeanUtil.toBean(info, FindAllWastePriceDTO.class);
            String stationName = stationInfoMapper.selectById(info.getStationId()).getStationName();
            findAllWastePriceDTO.setStationName(stationName);
            String typeName = wasteTypeMapper.selectById(info.getTypeId()).getTypeName();
            findAllWastePriceDTO.setTypeName(typeName);
            wastePriceDTOS.add(findAllWastePriceDTO);
        }
        AllFindAllWastePriceDTO dto = new AllFindAllWastePriceDTO();
        dto.setList(wastePriceDTOS);
        dto.setTotal(size);
        return dto;
    }
}
